using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;





	public class PopupWindowHelper :NativeWindow, IMessageFilter
	{
		#region Unmanaged Code
		[DllImport("user32", CharSet = CharSet.Auto)]
		private extern static int SendMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);

		[DllImport("user32", CharSet = CharSet.Auto)]
		private extern static int PostMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);

		private const int WM_ACTIVATE = 0x006;
		private const int WM_ACTIVATEAPP = 0x01C;
		private const int WM_NCACTIVATE = 0x086;

		[DllImport("user32")]
		private extern static void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private const int KEYEVENTF_KEYUP       = 0x0002;
		#endregion

		#region Member Variables

		private Form popup = null;

		private Form owner = null;

		private bool popupShowing = false;

	
		#endregion

  
		public void ShowPopup(Form owner, Form popup, Point location)
		{

			this.owner = owner;
			this.popup = popup;

            Application.AddMessageFilter(this);
			// Set the location of the popup form:
			popup.StartPosition = FormStartPosition.Manual;
			popup.Location = location;
            popup.Text = "";
            popup.FormBorderStyle = FormBorderStyle.Sizable;
			// Make it owned by the window that's displaying it:
			owner.AddOwnedForm(popup);			

			// Show the popup:
			this.popupShowing = true;
			popup.Show();
			popup.Activate();
			

		}


        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (this.popupShowing)
            {
                // check for WM_ACTIVATE and WM_NCACTIVATE
                if (m.Msg == WM_NCACTIVATE)
                {
                    // Check if the title bar will made inactive:
                    if (((int)m.WParam) == 0)
                    {
                        // If so reactivate it.
                        SendMessage(this.Handle, WM_NCACTIVATE, 1, IntPtr.Zero);

                   
                    }

                }
                else if (m.Msg == WM_ACTIVATEAPP)
                {
                    // Check if the application is being deactivated.
                    if ((int)m.WParam == 0)
                    {
                        // It is so cancel the popup:
                        ClosePopup();

                    }
                }
            }
        }


		public void ClosePopup()
		{
			if (this.popupShowing)
			{
                Application.RemoveMessageFilter(this);

				// Make sure the popup is closed and we've cleaned
				// up:
				this.owner.RemoveOwnedForm(this.popup);
				this.popupShowing = false;
				this.popup.Close();
		
		

				this.owner.Activate();
				
				// Null out references for GC
				this.popup = null;
				this.owner = null;
								
			}
		}


        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_NCLBUTTONDOWN = 0x0A1;
        private const int WM_NCRBUTTONDOWN = 0x0A4;
        private const int WM_NCMBUTTONDOWN = 0x0A7;

        #region IMessageFilter Members

        public bool PreFilterMessage(ref Message m)
        {
            if (this.popup != null)
            {
                switch (m.Msg)
                {
                    case WM_LBUTTONDOWN:
                    case WM_RBUTTONDOWN:
                    case WM_MBUTTONDOWN:
                    case WM_NCLBUTTONDOWN:
                    case WM_NCRBUTTONDOWN:
                    case WM_NCMBUTTONDOWN:
                        OnMouseDown();
                        break;
                }
            }
            return false;
        }

        private void OnMouseDown()
        {
            // Get the cursor location
            Point cursorPos = Cursor.Position;
            // Check if it is within the popup form
            if (!popup.Bounds.Contains(cursorPos))
            {
                // If not, then call to see if it should be closed
                ClosePopup();//(new PopupCancelEventArgs(popup, cursorPos));
            }
        }

        #endregion

    }




